package com.gastronomy.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.gastronomy.entity.DishesEntity;
import com.gastronomy.entity.ReplayInformationEntity;
import com.gastronomy.entity.User;
import com.gastronomy.service.DishesService;
import com.gastronomy.service.ReplayService;
import com.gastronomy.service.UserService;

@Controller
@RequestMapping("/dishes")
public class DishesController {

	@Resource
	private DishesService dishesService;
	
	@Resource
	private UserService userService;
	
	@Resource
	private ReplayService replayService;
	
	@RequestMapping("dishesDtail")
	public String toDishesDtail(HttpServletRequest request,
			HttpServletResponse response, Model model) {

		String dishesId = request.getParameter("id");
		DishesEntity dishesEntity = dishesService.queryDishesEntity(dishesId);
		if(dishesEntity ==null ){
			model.addAttribute("errorMsg","该美食不存在或者已经下架");
			return "loginUser/error";
		}
		
		dishesEntity.setFocuse(dishesEntity.getFocuse()+1);
		dishesService.updateDishesFocus(dishesEntity);
		List<ReplayInformationEntity> replayInformationEntities = replayService.queryReplayInformatins(dishesId,2);
		model.addAttribute("replayInformationEntities",replayInformationEntities);
		model.addAttribute("dishesEntity",dishesEntity);
		return "dish/dishesDetail";
	}

	
	@RequestMapping("/toBusinessHome")
	public String toBusinessHome(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		String id = request.getParameter("id");
		
		if(id == null || id.trim().equals("")){
			model.addAttribute("errorMsg","你进入的商户不存在");
			return "loginUser/error";
		}
		String currentPage = request.getParameter("currentPage");
		
		User user = userService.queryUserById(id);
		if(user == null){
			model.addAttribute("errorMsg","你进入的商户不存在");
			return "loginUser/error";
		}
		
		List<DishesEntity> result = dishesService.listDishes(user,currentPage,12);
		model.addAttribute("dishesEntities",result);
		model.addAttribute("user",user);
		return "dish/businessHome";
	}
}
